#include "common.h"

// Global Count of references to this DLL - defined in Main.cpp
extern UINT g_cDLLRefCount;

MyClassFactory::MyClassFactory(ExtensionType extensionType) :
m_cRef(1)
{
    m_ExtensionType = extensionType;
}

MyClassFactory::~MyClassFactory()
{
}

STDMETHODIMP_(ULONG) MyClassFactory::AddRef() 
{ 
    m_cRef++; 
    return m_cRef; 
}

STDMETHODIMP_(ULONG) MyClassFactory::Release()
{   
    m_cRef--; 
    if(m_cRef > 0) 
    { 
        return(m_cRef); 
    } 
    delete this; 
    return 0; 
}

HRESULT MyClassFactory::QueryInterface(REFIID riid, void** ppObject)
{
    HRESULT hr = S_OK;

    if(riid == IID_IUnknown || riid == IID_IClassFactory)
    {
        *ppObject= (IClassFactory*)this;
    }
    else
    {
        hr = E_NOINTERFACE;
        goto Error;
    }

    AddRef();

Error:
    return(hr);
}

HRESULT MyClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void** ppObject)
{
    HRESULT hr = S_OK;
    IObjectWithSite* pows = NULL;

    *ppObject = NULL;

    if(pUnkOuter != NULL)
    {
        hr = CLASS_E_NOAGGREGATION;
        goto Error;
    }

    //
    // Call MenuExtension::Create - this is where the MenuExtension is 
    // actually created
    //
    hr = MenuExtension::Create(&pows, m_ExtensionType);
    CHR(hr);
    CPR(pows);
        
    hr = pows->QueryInterface(riid, ppObject);
    CHR(hr);
    CPR(ppObject);

Error:
    RELEASE_OBJ(pows);    

    return hr;
}

HRESULT MyClassFactory::LockServer(BOOL fLock)
{
    if(fLock)
    {
        g_cDLLRefCount++;
    }
    else
    {
        g_cDLLRefCount--;
    }
    return(S_OK);
}
